import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import * as chalk from 'chalk';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';

import { AppModule } from './app.module';
import { IConfiguration } from './config/configuration';

async function bootstrap() {
  // const app = await NestFactory.create(AppModule, {
  //   bufferLogs: true,
  // });

  const app = await NestFactory.create<NestExpressApplication>(AppModule, { bufferLogs: true });

  const configService = app.get(ConfigService<IConfiguration>);

  const serverPort = configService.get('serverPort');

  const { localPath, prefix } = configService.get<IConfiguration['staticServer']>('staticServer');

  app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));

  app.useStaticAssets(localPath, { prefix: `/${prefix}` });

  app.listen(serverPort, () => {
    const baseServerUrl = `http://127.0.0.1:${serverPort}`;

    console.log('\r');
    console.log(chalk.bgRed('nestjs服务地址是：'), chalk.greenBright(baseServerUrl));
    console.log(chalk.bgBlue('静态文件服务地址：'), chalk.greenBright(`${baseServerUrl}/${prefix}`));
    console.log(chalk.bgGrey('本地手动调试地址：'), chalk.greenBright(`${baseServerUrl}/api/open/[folder]/[platform]`));
    console.log('\r');
  });
}
bootstrap();
